// index.js
// 获取应用实例
const app = getApp()
// 在页面中定义插屏广告
let interstitialAd = null

Page({
  data: {
    scene: app.globalData.scene,
    hidden: false, // 加载效果
    tid: 21, // 初始加载电影
    TopCur: 0, // 头部导航栏切换
    TabCur: 0, // 底部导航栏切换
    scrollLeft: 0,
    navlist: [{
        id: '0',
        tid: '21',
        name: '电影',
        sub: [{
            tid: '21',
            name: '动作片'
          },
          {
            tid: '22',
            name: '喜剧片'
          },
          {
            tid: '23',
            name: '爱情片'
          },
          {
            tid: '24',
            name: '科幻片'
          },
          {
            tid: '25',
            name: '恐怖片'
          },
          {
            tid: '26',
            name: '犯罪片'
          },
          {
            tid: '27',
            name: '战争片'
          },
          {
            tid: '29',
            name: '剧情片'
          },
          {
            tid: '30',
            name: '记录片'
          },
        ]
      },
      {
        id: '1',
        tid: '32',
        name: '电视剧',
        sub: [{
            tid: '32',
            name: '国产剧'
          },
          {
            tid: '33',
            name: '香港剧'
          },
          {
            tid: '34',
            name: '日本剧'
          },
          {
            tid: '35',
            name: '欧美剧'
          }
        ]
      },
      {
        id: '2',
        tid: '42',
        name: '动漫',
        sub: [{
            tid: '42',
            name: '国产动漫'
          },
          {
            tid: '43',
            name: '日本动漫'
          },
          {
            tid: '44',
            name: '欧美动漫'
          },
          {
            tid: '45',
            name: '海外动漫'
          }
        ]
      },
      {
        id: '3',
        tid: '37',
        name: '综艺',
        sub: [{
            tid: '37',
            name: '大陆综艺'
          },
          {
            tid: '38',
            name: '日韩综艺'
          },
          {
            tid: '39',
            name: '港台综艺'
          },
          {
            tid: '40',
            name: '欧美综艺'
          }
        ]
      }
    ]
  },
  onLoad() {
    if(app.globalData.scene == '1154'){
      // 取数据
      // console.log(JSON.parse(decodeURIComponent(app.globalData.data)));
      let share = JSON.parse(decodeURIComponent(app.globalData.data));
      this.setData({
        hidden: true,
        total: share.total,
        vlist: share.vlist,
      })
    }
    // if(this.scene == '1154'){
    //   console.log("分享朋友圈");
    //   console.log(JSON.parse(e.query.data));
    //   this.setData({
    //     data: JSON.parse(e.query.data),
    //   })
    // }
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-f2c3d256e996f22e'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
    var that = this;
    var url = app.globalData.baseurl;
    // console.log(that.data.tid);
    wx.request({
      url: url + '?ac=videolist&t=' + that.data.tid, //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          total: res.data.total,
          vlist: res.data.list,
        })
        console.log(res.data)
      },
      complete(res) {
        that.setData({
          hidden: true
        })
      }
    })
  },
  tabSelect(e) {
    var that = this;
    that.setData({
      hidden: false,
      // vlist: ''
    })
    var url = app.globalData.baseurl;
    that.setData({
      TabCur: e.currentTarget.dataset.pid,
      TopCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    console.log(e.currentTarget.dataset.tid);
    wx.request({
      url: url + '?ac=videolist&t=' + e.currentTarget.dataset.tid,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          total: res.data.total,
          vlist: res.data.list,
          hidden: true
        })
        console.log(res.data)
      }
    })
  },
  search() {
    wx.navigateTo({
      url: '../../pages/search/search',
    })
  },
  detail(e) {
    wx.navigateTo({
      url: '../../pages/detail/detail?ids=' + e.currentTarget.dataset.ids,
    })
  },
  tomeituan(){
    wx.navigateToMiniProgram({
      appId: 'wxde8ac0a21135c07d',
      path: '/index/pages/h5/h5?lch=cps:waimai:5:d5b21e99f7d05aa9a9fbb240c5407336:xingbayingyuan:2:129440&weburl=https%3A%2F%2Fdpurl.cn%2Fn5vdvGjz&f_userId=1&f_token=1',
      success(res) {
        console.log("打开成功");
      }
    })
  },
  onShareAppMessage: function () {
    return {
    }
  },
  onShareTimeline: function () {

    return {
      query: "data=" + encodeURIComponent(JSON.stringify(this.data))
    }
  },

})